﻿using System;
using System.Collections.Generic;

using ApprovalTests;
using ApprovalTests.Reporters;

using FluentAssertions;

using JsonFx.Json;
using JsonFx.Serialization;

using NewModel.Accounting.Calculation;
using NewModel.Accounting.Core;
using NewModel.Accounting.Persistence;

using NUnit.Framework;

namespace NewModel.UnitTests.Accounting.Persistence
{
    [TestFixture]
    [UseReporter(typeof(VisualStudioReporter))]
    public sealed class TestTransactionsLoader
    {
        static readonly Batch sBatch = new Batch
        {
            IngressBalance = new List<AccountTotal>
                {
                    new AccountTotal
                    {
                        Account = Account.Asset,
                        Total = new Total
                        {
                            Credit = new MoneyBag(63*Currency.Usd),
                            Debit = new MoneyBag(67*Currency.Usd),
                        }
                    }
                },
            ReferenceInterval = new Interval(new DateTime(2000, 1, 1), new DateTime(2000, 12, 31)),
            Transactions = new List<Transaction>
                {
                    new Transaction(new DateTime(2000, 1, 1), "comment", 
                        2*Currency.Usd, Account.Asset, Account.Expense)
                },
            ExchangeRates = new List<ExchangeRate>
                {
                    new ExchangeRate(new DateTime(2000, 1, 1), 
                        Currency.Byr, Currency.Usd, 9500, 1, 
                        ExchangeRateSources.Nbrb)
                }
        };
        static readonly BatchData sBatchData = new BatchData(sBatch);

        static readonly IDataWriter sWriter = new JsonWriter(
            new DataWriterSettings
            {
                PrettyPrint = true,
            });

        static readonly IDataReader sReader = new JsonReader();

        [Test]
        public void JsonSerialization()
        {
            Approvals.Verify(sWriter.Write(sBatchData));
        }
        [Test]
        public void LedgerData_Should_Deserialize_Fine()
        {
            sReader.Read<BatchData>(sWriter.Write(sBatchData))
                .ShouldBeEquivalentTo(sBatchData);
        }
        [Test]
        public void LedgerData_Should_Convert_Nicely_Back_To_Ledger()
        {
            var context = new Context();
            context.AccountByKey[Account.Asset.Name] = Account.Asset;
            context.AccountByKey[Account.Expense.Name] = Account.Expense;
            context.CurrencyByKey[Currency.Usd.Name] = Currency.Usd;
            context.CurrencyByKey[Currency.Byr.Name] = Currency.Byr;
            sBatchData.Back(context).ShouldBeEquivalentTo(sBatch);
        }
    }
}